package ui.fragment.setting;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;

import java.util.Locale;

import controller.company.SelectCompanyAsync;
import controller.setting.DownloadImageCompanyLogoInSettingAsync;
import controller.setting.SettingAsync;
import de.hdodenhof.circleimageview.CircleImageView;
import model.settting.SettingItem;
import ui.activity.login.Login;
import utils.Utils;

public class SettingFragment extends Fragment
        implements View.OnClickListener, View.OnTouchListener{

    /**
     * String section
     */

    /**
     * View section
     */
    private static CircleImageView circleIvCompanyLogoInSettingFragment;

    private static EditText mEtFormalNameInSettingFragment;
    private static EditText mEtNameInSettingFragment;
    private static EditText mEtDescriptionInSettingFragment;
    private static EditText mEtAddressInSettingFragment;
    private static EditText mEtCityInSettingFragment;
    private static EditText mEtStateInSettingFragment;
    private static EditText mEtPostcodeInSettingFragment;
    private static EditText mEtCountryInSettingFragment;
    private static EditText mEtMobileCountryCodeInSettingFragment;
    private static EditText mEtPhoneNumberInSettingFragment;
    private static EditText mEtOfficeCountryCodeInSettingFragment;
    private static EditText mEtOfficeNumberInSettingFragment;
    private static EditText mEtEmployeePassInSettingFragment;
    private static EditText mEtAffliationsInSettingFragment;
    private static EditText mEtPaypalAccountInSettingFragment;

    private LinearLayout mLlInSettingFragment;

    //the others session
    public static Context mContext;

    public static Fragment newInstance() {
        SettingFragment fragment = new SettingFragment();
        return fragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.circleiv_company_logo_in_setting_fragment:
                /**todo
                 * Transfer to Select Company logo to change the current logo
                 */

                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = getLayoutInflater(savedInstanceState).inflate(
                R.layout.fragment_setting, container, false);
        mContext = getActivity();
        // Initial views
        initialViews(v);
        initialData();
        setDataForSettingFragment();
        disableSettingFragment();
//        getCountryName();
        if (Enterprise.setting.getSettingItem() != null)
        SettingFragment.displayDataToViewSettingFragment(Enterprise.setting.getSettingItem());
        return v;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //TODO will remove after finish all ibtn on actionbar
        //TODO mean write function invisible all the others ibtn and only visible ibtn at current page
        //invisible ibtn create group
        Enterprise.mIbtnCreateGroup.setVisibility(View.GONE);
        Enterprise.mIbtnUpload.setVisibility(View.VISIBLE);
        Enterprise.mIbtnInviteUserToCompany.setVisibility(View.GONE);
    }

    @Override
    public boolean onTouch(View v, MotionEvent motionEvent) {
        switch (v.getId()) {
            case R.id.ll_setting_fragment:
                enableSettingFragment();
                hideSoftKeyBoardInSettingFragment();
                break;
        }
        return false;
    }

    /**
     * Initialize methods
     */
    private void initialData() {
        circleIvCompanyLogoInSettingFragment.setOnClickListener(this);

        mLlInSettingFragment.setOnTouchListener(this);
        //visible all ibtns
        Enterprise.mIbtnCreateGroup.setVisibility(View.GONE);
        Enterprise.mIbtnUpload.setVisibility(View.INVISIBLE);
        Enterprise.mIbtnInviteUserToCompany.setVisibility(View.GONE);
    }
    private void initialViews(View v) {
        circleIvCompanyLogoInSettingFragment = (CircleImageView)v.
                findViewById(R.id.circleiv_company_logo_in_setting_fragment);
        mEtFormalNameInSettingFragment = (EditText)v.
                findViewById(R.id.et_formal_name_in_setting_fragment);
        mEtNameInSettingFragment = (EditText)v.
                findViewById(R.id.et_name_in_setting_fragment);
        mEtDescriptionInSettingFragment = (EditText)v.
                findViewById(R.id.et_description_in_setting_fragment);
        mEtAddressInSettingFragment = (EditText)v.
                findViewById(R.id.et_address_in_setting_fragment);
        mEtCityInSettingFragment = (EditText) v.
                findViewById(R.id.et_city_in_setting_fragment);
        mEtStateInSettingFragment = (EditText)v.
                findViewById(R.id.et_state_in_setting_fragment);
        mEtPostcodeInSettingFragment = (EditText)v.
                findViewById(R.id.et_postcode_in_setting_fragment);
        mEtCountryInSettingFragment = (EditText)v.
                findViewById(R.id.et_country_in_setting_fragment);
        mEtMobileCountryCodeInSettingFragment = (EditText)v.
                findViewById(R.id.et_mobile_country_code_in_setting_fragment);
        mEtPhoneNumberInSettingFragment = (EditText)v.
                findViewById(R.id.et_phone_number_in_setting_fragment);
        mEtOfficeCountryCodeInSettingFragment = (EditText)v.
                findViewById(R.id.et_office_country_code_in_setting_fragment);
        mEtOfficeNumberInSettingFragment = (EditText)v.
                findViewById(R.id.et_office_number_in_setting_fragment);
        mEtEmployeePassInSettingFragment = (EditText)v.
                findViewById(R.id.et_employee_pass_in_setting_fragment);
        mEtAffliationsInSettingFragment = (EditText)v.
                findViewById(R.id.et_affliations_in_setting_fragment);
        mEtPaypalAccountInSettingFragment = (EditText)v.
                findViewById(R.id.et_paypal_account_in_setting_fragment);
        mLlInSettingFragment = (LinearLayout)v.
                findViewById(R.id.ll_setting_fragment);
    }

    /**
     * The others methods
     */

    private void disableSettingFragment() {
        circleIvCompanyLogoInSettingFragment.setEnabled(false);
        mEtFormalNameInSettingFragment.setEnabled(false);
        mEtNameInSettingFragment.setEnabled(false);
        mEtDescriptionInSettingFragment.setEnabled(false);
        mEtAddressInSettingFragment.setEnabled(false);
        mEtCityInSettingFragment.setEnabled(false);
        mEtStateInSettingFragment.setEnabled(false);
        mEtPostcodeInSettingFragment.setEnabled(false);
        mEtCountryInSettingFragment.setEnabled(false);
        mEtMobileCountryCodeInSettingFragment.setEnabled(false);
        mEtPhoneNumberInSettingFragment.setEnabled(false);
        mEtOfficeCountryCodeInSettingFragment.setEnabled(false);
        mEtOfficeNumberInSettingFragment.setEnabled(false);
        mEtEmployeePassInSettingFragment.setEnabled(false);
        mEtAffliationsInSettingFragment.setEnabled(false);
        mEtPaypalAccountInSettingFragment.setEnabled(false);
    }

    public static void displayDataToViewSettingFragment (SettingItem settingItem) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            new DownloadImageCompanyLogoInSettingAsync(mContext, circleIvCompanyLogoInSettingFragment).executeOnExecutor(
                    AsyncTask.THREAD_POOL_EXECUTOR, settingItem.getCompanyLogo());
        else
            new DownloadImageCompanyLogoInSettingAsync(mContext, circleIvCompanyLogoInSettingFragment).execute(
                    settingItem.getCompanyLogo());

        mEtFormalNameInSettingFragment.setText(settingItem.getFormalName());
        mEtNameInSettingFragment.setText(settingItem.getCompanyName());
        mEtDescriptionInSettingFragment.setText(settingItem.getDescription());
        mEtAddressInSettingFragment.setText(settingItem.getAddress());
        mEtCityInSettingFragment.setText(settingItem.getCity());
        mEtStateInSettingFragment.setText(settingItem.getState());
        mEtPostcodeInSettingFragment.setText(String.valueOf(settingItem.getPostCode()));
        //TODO get country name from country id at here: use library
        mEtCountryInSettingFragment.setText(settingItem.getCountryId());
        mEtMobileCountryCodeInSettingFragment.setText(String.valueOf(settingItem.getMobileCountryCode()));
        mEtPhoneNumberInSettingFragment.setText(String.valueOf(settingItem.getPhoneNumber()));
        mEtOfficeCountryCodeInSettingFragment.setText(String.valueOf(settingItem.getOfficeCountryCode()));
        mEtOfficeNumberInSettingFragment.setText(String.valueOf(settingItem.getOfficeNumber()));
        mEtEmployeePassInSettingFragment.setText(String.valueOf(settingItem.getEmployeePass()));
        mEtAffliationsInSettingFragment.setText(settingItem.getAffliations());
        mEtPaypalAccountInSettingFragment.setText(String.valueOf(settingItem.getPaypalAddress()));
    }

    private void enableSettingFragment() {
        circleIvCompanyLogoInSettingFragment.setEnabled(true);
        mEtFormalNameInSettingFragment.setEnabled(true);
        mEtNameInSettingFragment.setEnabled(true);
        mEtDescriptionInSettingFragment.setEnabled(true);
        mEtAddressInSettingFragment.setEnabled(true);
        mEtCityInSettingFragment.setEnabled(true);
        mEtStateInSettingFragment.setEnabled(true);
        mEtPostcodeInSettingFragment.setEnabled(true);
        mEtCountryInSettingFragment.setEnabled(true);
        mEtMobileCountryCodeInSettingFragment.setEnabled(true);
        mEtPhoneNumberInSettingFragment.setEnabled(true);
        mEtOfficeCountryCodeInSettingFragment.setEnabled(true);
        mEtOfficeNumberInSettingFragment.setEnabled(true);
        mEtEmployeePassInSettingFragment.setEnabled(true);
        mEtAffliationsInSettingFragment.setEnabled(true);
        mEtPaypalAccountInSettingFragment.setEnabled(true);
    }

    private void getCountryName() {
        for (String locales : Locale.getISOCountries()) {
            Locale mLocale = new Locale("", locales);
            Log.i("","Country code: " + mLocale.getCountry());
            Log.i("","Country name: " + mLocale.getDisplayCountry());
        }
    }

    private void hideSoftKeyBoardInSettingFragment() {
        Utils.hideSoftKeyboard(mContext, mEtFormalNameInSettingFragment);
        Utils.hideSoftKeyboard(mContext, mEtNameInSettingFragment);
        Utils.hideSoftKeyboard(mContext, mEtDescriptionInSettingFragment);
        Utils.hideSoftKeyboard(mContext, mEtAddressInSettingFragment);
        Utils.hideSoftKeyboard(mContext, mEtCityInSettingFragment);
        Utils.hideSoftKeyboard(mContext, mEtStateInSettingFragment);
        Utils.hideSoftKeyboard(mContext, mEtPostcodeInSettingFragment);
        Utils.hideSoftKeyboard(mContext, mEtCountryInSettingFragment);
        Utils.hideSoftKeyboard(mContext, mEtMobileCountryCodeInSettingFragment);
        Utils.hideSoftKeyboard(mContext, mEtPhoneNumberInSettingFragment);
        Utils.hideSoftKeyboard(mContext, mEtOfficeCountryCodeInSettingFragment);
        Utils.hideSoftKeyboard(mContext, mEtOfficeNumberInSettingFragment);
        Utils.hideSoftKeyboard(mContext, mEtEmployeePassInSettingFragment);
        Utils.hideSoftKeyboard(mContext, mEtAffliationsInSettingFragment);
        Utils.hideSoftKeyboard(mContext, mEtPaypalAccountInSettingFragment);
    }

    private void setDataForSettingFragment() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            new SettingAsync(getActivity()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                    String.valueOf(Login.user.getUserInfo().getUserGlobalId()),
                    Login.user.getUserInfo().getAccessToken(),
                    String.valueOf(Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyId()),
                    String.valueOf(Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyIdGroup()));
        else
            new SettingAsync(getActivity()).execute(
                    String.valueOf(Login.user.getUserInfo().getUserGlobalId()),
                    Login.user.getUserInfo().getAccessToken(),
                    String.valueOf(Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyId()),
                    String.valueOf(Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyIdGroup()));
    }
}
